package com.newenergy.wisehome.ui.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.MyEvent;
import com.newenergy.wisehome.common.helper.AppInfoHelper;
import com.newenergy.wisehome.common.helper.SPUtil;
import com.newenergy.wisehome.common.helper.ToastUtil;
import com.newenergy.wisehome.config.Constants;
import com.newenergy.wisehome.update.CheckVersionInfoTask;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

import static com.newenergy.wisehome.config.Constants.ISDOWNLOAD;

/**
 * Created by new on 2017/12/26.
 */

public class AboutUsFragment extends BaseFragment {


    @BindView(R.id.iv_icon)
    ImageView mIvIcon;
    @BindView(R.id.tv_version)
    TextView mTvVersion;
    @BindView(R.id.btn_checkversion)
    Button mBtnCheckversion;
    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_about_us;
    }

    public static ISupportFragment newInstace() {
        return new AboutUsFragment();
    }


    @Override
    public void init(View rootView) {
        registerEventBus();
        String versionName = getString(R.string.version_code) + AppInfoHelper.getVersionName
                (getContext());
        String appName = getString(getContext().getApplicationInfo().labelRes);
        int icon = getContext().getApplicationInfo().icon;

        mTvPage.setText(getString(R.string.about));
        mIvIcon.setBackgroundResource(icon);
        mTvVersion.setText(versionName);

    }


    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_checkversion})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();

                break;
            case R.id.tv_back:
                doBack();

                break;
            case R.id.btn_checkversion:
                boolean isDownload = SPUtil.getBool(ISDOWNLOAD, false);
                if (!isDownload){
                    new CheckVersionInfoTask(getContext(), true).execute();
                }else {
                    ToastUtil.toast(getString(R.string.is_being_updated));
                }

                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(MyEvent event) {
        String tag = event.getTag();
        String data = (String)event.getData();
        switch (tag) {
            case Constants.NONEWVERSION:
                ToastUtil.toast(data);
                break;

        }
    }


}
